package com.open.capacity.firebird.controller;

import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.model.*;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.firebird.service.FireBirdService;
import com.open.capacity.firebird.util.WordGenerate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/fireBird")
public class FireBirdController {

    @Autowired
    FireBirdService fireBirdService;

    @Value("${export.outPath}")
    private String outPath;
    @Value("${export.tmpl.path}")
    private String tmplPath;
    @Value("${export.tmpl.word.univInfo}")
    private String univInfoWordTmpl = "univ_info_tmpl.ftl";
    @Value("${export.tmpl.word.regInfo}")
    private String regInfoWordTmpl = "bmxx_tmpl.ftl";
    @Value("${export.tmpl.word.phyExmInfo}")
    private String phyExmInfoWordTmpl = "tjxx_tmpl.ftl";
    @Value("${export.tmpl.word.acheVolInfo}")
    private String acheVolInfoWordTmpl = "cjzyxx_tmpl.ftl";

    /**
     * 尝试与Firebird数据库文件建立连接
     * @param db
     * @return
     */
    @PostMapping("/connectFireBird")
    public Result connectFireBird(@RequestBody DB db) {
        try {
            return fireBirdService.connectFireBird(db);
        } catch (ControllerException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 进行连接测试
     * @param db
     * @return
     */
    @PostMapping("/testConnect")
    public Result testConnect(@RequestBody DB db) {
        try {
            Result result = fireBirdService.testConnect(db);
            //切换数据库为默认数据库
//        DataSourceUtil.clear();
            return result;
        } catch (ControllerException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 获取该数据库中的所有表名
     * @param db
     * @return
     */
    @PostMapping("/getTables")
    public List<String> getTables(@RequestBody DB db) {
        try {
            return fireBirdService.getTables(db);
        } catch (ControllerException e) {
            throw new ControllerException(e);
        }

    }

    /**
     * 获取该数据库中的某个数据表中的所有字段属性
     * @param map
     * @return
     */
    @PostMapping("/getTableFields")
    public List<TableField> getTableFields(@RequestParam Map map) {
        try {
            return fireBirdService.getTableFields(map);
        } catch (ControllerException e) {
            throw new ControllerException(e);
        }

    }

    /**
     * 获取成绩与志愿信息
     * @param map
     * @return
     */
    @GetMapping("/getAcheAndVolInfoList")
    public PageResult<AcheAndVolInfo> getAcheAndVolInfoList(@RequestParam Map map) {
        try {
            return fireBirdService.getAcheAndVolInfoList(map);
        } catch (ControllerException e) {
            throw new ControllerException(e);
        }

    }

    /**
     * 获取学生体检信息
     * @param map
     * @return
     */
    @GetMapping("/getPhyExmInfoList")
    public PageResult<PhyExmInfo> getPhyExmInfoList(@RequestParam Map map) {
        try {
            return fireBirdService.getPhyExmInfoList(map);
        } catch (ControllerException e) {
            throw new ControllerException(e);
        }

    }

    /**
     * 获取报名信息
     * @param map
     * @return
     */
    @GetMapping("/getRegisterInfoList")
    public PageResult<RegisterInfo> getRegisterInfoList(@RequestParam Map map) {
        try {
            return fireBirdService.getRegisterInfoList(map);
        } catch (ControllerException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 获取院校信息
     * @return
     */
    @GetMapping("/getUnivInfo")
    public Result getUnivInfo() {
        try {
            return fireBirdService.getUnivInfo();
        } catch (ControllerException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 获取录取及格信息
     * @param map
     * @return
     */
    @GetMapping("/getAdmInfoList")
    public PageResult getAdmInfoList(@RequestParam Map map){
        try {
            return fireBirdService.getAdmInfoList(map);
        }catch (ControllerException e){
            throw new ControllerException(e);
        }
    }

    /**
     * 导出院校信息
     * @param map
     * @param response
     * @return
     */
    @PostMapping("/exportUnivInfo")
    public Result exportUnivInfo(@RequestBody Map map, HttpServletResponse response) {
        try {
            String fileName = "院校信息.doc";
            WordGenerate.WordGenerate(map, outPath, tmplPath, univInfoWordTmpl, fileName, response);
            return Result.succeed("导出成功，请在" + outPath + "下查看文件" + fileName);
        } catch (ControllerException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 导出报名信息
     * @param list
     * @param response
     * @return
     */
    @PostMapping("/exportRegInfo")
    public Result exportRegInfo(@RequestBody String list, HttpServletResponse response) {
        try {
            String fileName = "报名信息.doc";
            return fireBirdService.export(list, outPath, tmplPath, fileName, regInfoWordTmpl, response);
        } catch (ControllerException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 导出体检信息
     * @param list
     * @param response
     * @return
     */
    @PostMapping("/exportPhyExmInfo")
    public Result exportPhyExmInfo(@RequestBody String list, HttpServletResponse response) {
        try {
            String fileName = "体检信息.doc";
            return fireBirdService.export(list, outPath, tmplPath, fileName, phyExmInfoWordTmpl, response);
        } catch (ControllerException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 导出成绩与志愿信息
     * @param list
     * @param response
     * @return
     */
    @PostMapping("/exportAcheVolInfo")
    public Result batchExportAcheVolInfo(@RequestBody String list, HttpServletResponse response) {
        try {
            String fileName = "成绩志愿信息.doc";
            return fireBirdService.export(list, outPath, tmplPath, fileName, acheVolInfoWordTmpl, response);
        } catch (ControllerException e) {
            throw new ControllerException(e);
        }
    }

    /**
     * 获取成绩分数段人数
     * @param map
     * @return
     */
    @PostMapping("/getGradeCount")
    public Result getGradeCount(@RequestBody Map map){
        return fireBirdService.getGradeCount(map);
    }

    /**
     * 设置体检结论图表数据
     * @return
     */
    @GetMapping("/setPhyExmConChart")
    public Result setPhyExmConChart(){
        return fireBirdService.setPhyExmConChart();
    }

    /**
     * 设置录取及格图表数据
     * @return
     */
    @GetMapping("/setAdmissionPassChart")
    public Result setAdmissionPassChart(){
        return fireBirdService.setAdmissionPassChart();
    }

    /**
     * 获取录取查询结果
     * @param map
     * @return
     */
    @PostMapping("/admissionQuery")
    public Result admissionQuery(@RequestBody Map map){
        return fireBirdService.admissionQuery(map);
    }

    /**
     * 获取证件类型列表
     * @return
     */
    @GetMapping("/getDocTypeList")
    public List<DocType> getDocTypeList() {
        return fireBirdService.getDocTypeList();
    }
}
